package root.agent.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import root.agent.demo.invoke.SpringAiInvokeV2;

@RestController
@RequestMapping("/api/chat")
public class ChatController {

    @Autowired
    private SpringAiInvokeV2 springAiInvokeV2;

    @GetMapping(value = "/stream/{text}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> streamChat(@PathVariable String text) {
        return springAiInvokeV2.getChatStream(text)
                .map(content -> ServerSentEvent.<String>builder()
                        .data(content)
                        .build());
    }

    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ServerSentEvent<String>> streamChat2() {
        return springAiInvokeV2.getChatStream("我失恋了")
                .map(content -> ServerSentEvent.<String>builder()
                        .data(content)
                        .build());
    }
}
